package com.yunchao.feature.business.data.datasource.api.request

import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import com.yunchao.feature.business.data.datasource.api.request.BusinessRequest.Companion.SUCCESS_CODE
import com.yunchao.feature.business.data.datasource.api.request.BusinessRequest.Companion.SUCCESS_MESSAGE
import com.yunchao.feature.business.domain.enum.BusinessType
import com.yunchao.feature.business.domain.model.BusinessInfoModel
import com.yunchao.feature.business.domain.model.BusinessPackModel

data class BusinessRequest(
    val tranTypeId: String,
    val message: String
) {
    companion object {
        const val SUCCESS_MESSAGE: String = "success"
        const val SUCCESS_CODE = "000"
    }
}


internal fun getMIMISIF002RequestInfo(
    gson: Gson,
    taskInfo: BusinessInfoModel,
    items: List<BusinessPackModel>
) = BusinessRequest(
    tranTypeId = taskInfo.tranTypeID,
    message = gson.toJson(
        MIMISIF002Request(
            tranTypeID = BusinessType.BusinessTypeMIMISIF002.type,
            respMessage = SUCCESS_MESSAGE,
            respCode = SUCCESS_CODE,
            tranID = taskInfo.tranID,
            chkUserId = taskInfo.chkUserId,
            sequency = taskInfo.sequency,
            packInfoList = items.map {
                MIMISIF002Request.PackInfoRequest(
                    sackNo = it.lockEPCInfo?.id,
                    voucherTypeId = it.voucherTypeId,
                    voucherTypeName = it.voucherTypeName,
                    value = it.value,
                    operatorID = taskInfo.giveOperator,
                    oprDT = it.oprDT,
                )
            }
        )
    )
)

internal fun getMIMISIF004RequestInfo(
    gson: Gson,
    taskInfo: BusinessInfoModel,
    items: List<BusinessPackModel>
) = BusinessRequest(
    tranTypeId = taskInfo.tranTypeID,
    message = gson.toJson(
        MIMISIF004Request(
            tranTypeID = BusinessType.BusinessTypeMIMISIF004.type,
            respMessage = SUCCESS_MESSAGE,
            respCode = SUCCESS_CODE,
            tranID = taskInfo.tranID,
            sequency = taskInfo.sequency,
            packInfoList = items.map {
                MIMISIF004Request.PackInfoRequest(
                    sackNo = it.sackNo,
                    voucherTypeId = it.voucherTypeId,
                    voucherTypeName = it.voucherTypeName,
                    value = it.value,
                    operatorID = taskInfo.giveOperator,
                    oprDT = it.oprDT,
                    paperTypeName = it.paperTypeName,
                    paperTypeId = it.paperTypeID,
                    bundles = it.bundles,
                    tie = it.tie,
                    sackMoney = it.sackMoney,
                )
            }
        )
    )
)

internal fun getMIMISIF006RequestInfo(
    gson: Gson,
    taskInfo: BusinessInfoModel,
    items: List<BusinessPackModel>
) = BusinessRequest(
    tranTypeId = taskInfo.tranTypeID,
    message = gson.toJson(
        MIMISIF006Request(
            tranTypeID = BusinessType.BusinessTypeMIMISIF006.type,
            respMessage = SUCCESS_MESSAGE,
            respCode = SUCCESS_CODE,
            tranID = taskInfo.tranID,
            chkUserId = taskInfo.chkUserId,
            sequency = taskInfo.sequency,
            unPackInfoList = items.map {
                MIMISIF006Request.PackInfoRequest(
                    sackNo = it.sackNo,
                    voucherTypeId = it.voucherTypeId,
                    voucherTypeName = it.voucherTypeName,
                    value = it.value,
                    oprDT = it.oprDT,
                    paperTypeName = it.paperTypeName,
                    paperTypeId = it.paperTypeID,
                    bundles = it.bundles,
                    tie = it.tie,
                    sackMoney = it.sackMoney,
                )
            }
        )
    )
)


internal fun getMIMISIF008RequestInfo(
    gson: Gson,
    taskInfo: BusinessInfoModel,
    items: List<BusinessPackModel>
) = BusinessRequest(
    tranTypeId = taskInfo.tranTypeID,
    message = gson.toJson(
        MIMISIF008Request(
            tranTypeID = BusinessType.BusinessTypeMIMISIF008.type,
            respMessage = SUCCESS_MESSAGE,
            respCode = SUCCESS_CODE,
            tranID = taskInfo.tranID,
            chkUserId = taskInfo.chkUserId,
            sequency = taskInfo.sequency,
            transferDataList = items.map {
                MIMISIF008Request.PackInfoRequest(
                    sackNo = it.sackNo,
                    voucherTypeId = it.voucherTypeId,
                    voucherTypeName = it.voucherTypeName,
                    value = it.value,
                    oprDT = it.oprDT,
                    paperTypeName = it.paperTypeName,
                    paperTypeId = it.paperTypeID,
                    bundles = it.bundles,
                    tie = it.tie,
                    sackMoney = it.sackMoney,
                    mustOutFlag = it.mustOutFlag,
                )
            }
        )
    )
)

data class MIMISIF002Request(
    val tranTypeID: String?,
    val respMessage: String?,
    val respCode: String?,
    val tranID: String?,
    val chkUserId: String?,
    val sequency: String?,
    val packInfoList: List<PackInfoRequest> = emptyList()
) {
    data class PackInfoRequest(
        val sackNo: String?,
        val voucherTypeId: String?,
        val voucherTypeName: String?,
        @SerializedName("val") val value: String?,
        val operatorID: String?,
        val oprDT: String?,
    )
}

data class MIMISIF004Request(
    val tranTypeID: String?,
    val respMessage: String?,
    val respCode: String?,
    val tranID: String?,
    val sequency: String?,
    val packInfoList: List<PackInfoRequest> = emptyList()
) {
    data class PackInfoRequest(
        val sackNo: String?,
        val voucherTypeId: String?,
        val voucherTypeName: String?,
        val paperTypeId: String?,
        val paperTypeName: String?,
        @SerializedName("val") val value: String?,
        val bundles: String?,
        val tie: String?,
        val sackMoney: String?,
        val operatorID: String?,
        val oprDT: String?,
    )
}

data class MIMISIF006Request(
    val tranTypeID: String?,
    val respMessage: String?,
    val respCode: String?,
    val tranID: String?,
    val chkUserId: String?,
    val sequency: String?,
    val unPackInfoList: List<PackInfoRequest> = emptyList()
) {
    data class PackInfoRequest(
        val sackNo: String?,
        val voucherTypeId: String?,
        val voucherTypeName: String?,
        val paperTypeId: String?,
        val paperTypeName: String?,
        @SerializedName("val") val value: String?,
        val bundles: String?,
        val tie: String?,
        val sackMoney: String?,
        val oprDT: String?,
    )
}

data class MIMISIF008Request(
    val tranTypeID: String?,
    val respMessage: String?,
    val respCode: String?,
    val tranID: String?,
    val chkUserId: String?,
    val sequency: String?,
    val transferDataList: List<PackInfoRequest> = emptyList()
) {
    data class PackInfoRequest(
        val sackNo: String?,
        val voucherTypeId: String?,
        val voucherTypeName: String?,
        val paperTypeId: String?,
        val paperTypeName: String?,
        @SerializedName("val") val value: String?,
        val bundles: String?,
        val tie: String?,
        val sackMoney: String?,
        val mustOutFlag: String?,
        val oprDT: String?,
    )
}